package com.menu.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.menu.Mapper.AttentionMapper;
import com.menu.Mapper.UserMapper;
import com.menu.Pojo.Attention;
import com.menu.Pojo.MenuFather;
import com.menu.Pojo.User;
import com.menu.Service.AttentionService;
import com.menu.Service.NoticeService;
import com.menu.Utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AttentionServiceImpl implements AttentionService {
    @Resource
    AttentionMapper attentionMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    NoticeService noticeService;


    @Override
    public ResponseResult attention(Integer userOneId, Integer userTwoId) {
        User user= userMapper.findByUserId(userOneId);
        if (user == null) {
            return ResponseResult.error("用户1(关注者)不存在");
        }
        User user1=userMapper.findByUserId(userTwoId);
        if (user1 == null) {
            return ResponseResult.error("用户2(被关注者)不存在");
        }
        if(userOneId.equals(userTwoId)){
            return ResponseResult.error("用户不能关注自己");


        }
        Attention attention= attentionMapper.findByUserOneIdAndUserTwoId(userOneId, userTwoId);
        if (attention == null) {
        attentionMapper.addAttention(userOneId, userTwoId);
        noticeService.addAttentionUserNotice(userOneId,userTwoId);
        Integer i=attentionMapper.countByUserTwoId(userTwoId);
        return ResponseResult.success("关注成功",i);
        }else{
            attentionMapper.deleteAttention(userOneId, userTwoId);
            Integer i=attentionMapper.countByUserTwoId(userTwoId);
            return ResponseResult.success("取消关注成功",i);
        }

    }

    @Override
    public ResponseResult judgeOfAttention(Integer userOneId, Integer userTwoId) {
        User user= userMapper.findByUserId(userOneId);
        if (user == null) {
            return ResponseResult.error("用户1(关注者)不存在");
        }
        User user1=userMapper.findByUserId(userTwoId);
        if (user1 == null) {
            return ResponseResult.error("用户2(被关注者)不存在");
        }
        Attention attention= attentionMapper.findByUserOneIdAndUserTwoId(userOneId, userTwoId);
        if (attention == null) {
            return ResponseResult.success("没有关注",0);
        }else{
            return ResponseResult.success("已经关注了",1);
        }
    }

    @Override
    public ResponseResult findMyAttention(Integer userId,Integer pageNow,Integer pageSize) {
        User user= userMapper.findByUserId(userId);
        if (user == null) {
            return ResponseResult.error("用户不存在");
        }
        List<User> userList=new ArrayList<>();
        Integer[] i=attentionMapper.findUserTwoIdsByUerId(userId);
        for(int x:i){
            User user1=userMapper.findByUserId(x);
            userList.add(user1);
        }
        PageHelper.startPage(pageNow,pageSize);
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        return ResponseResult.success("遍历我的关注列表成功",pageInfo);
    }

    @Override
    public ResponseResult findMyFans(Integer userId,Integer pageNow,Integer pageSize) {
        User user= userMapper.findByUserId(userId);
        if (user == null) {
            return ResponseResult.error("用户不存在");
        }
        List<User> userList=new ArrayList<>();
        Integer[] i=attentionMapper.findUserOneIdByUserId(userId);
        for(int x:i){
            User user1=userMapper.findByUserId(x);
            userList.add(user1);
        }
        PageHelper.startPage(pageNow,pageSize);
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        return ResponseResult.success("遍历我的粉丝列表成功",pageInfo);
    }

    @Override
    public ResponseResult 调皮的接口() {
        attentionMapper.adds();
        Integer i=attentionMapper.countByUserTwoId(1);
        return ResponseResult.success("调皮不",i);
    }
}

